package com.nm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.nm.commom.Result;
import com.nm.mapper.AdminMapper;
import com.nm.pojo.Admin;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *管理员接口
 */
//@CrossOrigin
@RestController
@Api(tags = "管理员接口")
//@RequestMapping(value = "admin",produces = "text/html;charset=utf-8")
public class AdminController {

    @Autowired
    private AdminMapper adminMapper;

    /**
     * 查询全部管理员
     */
    //
    @RequestMapping(value = "/queryAll")
    public Result queryAll(){
        List<Admin> list1 = adminMapper.queryAll();
        System.out.println("~~~~~~~~~~~~~成功");
        return Result.success(list1);
    }

    /**
     *管理员注册
     * @apiNote id不用传
     */
    //
    @PostMapping(value = "/register")
    public Result admindl(@RequestBody Admin admin){
        int i = adminMapper.dlAdmin(admin);
        if (i==0){
            adminMapper.insert(admin);
            return Result.success();
        }else
            return Result.error(400,"重复注册");
    }

    /**
     *管理员登录
     * @apiNote id不用传
     */
    //
    @PostMapping (value = "/login")
    public Result dlAdmin(@RequestBody Admin admin){
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        String adminName = admin.getAdminName();
        String adminPwd = admin.getAdminPwd();
        wrapper.eq("admin_name",adminName)
                .eq("admin_pwd",adminPwd);
        Long aLong = adminMapper.selectCount(wrapper);
        System.out.println("AdminCount:"+aLong);
        if (aLong==1){
            return Result.success("登录成功");
        }else
            return Result.error(400,"登录失败");

    }

}
